<?php

namespace app\common\model;

use app\common\func\Constant;
use app\common\func\Lib;
use think\Model;


class Allocation extends Model
{

    

    

    // 表名
    protected $name = 'allocation';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'from_type_text',
        'to_type_text',
        'data_type_text',
        'confirm_type_text',
        'confirm_time_text',
        'create_time_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1')];
//        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getFromTypeList()
    {
        return ['1' => __('From_type 1'), '2' => __('From_type 2')];
    }

    public function getToTypeList()
    {
        return ['2' => __('To_type 2')];
//        return ['2' => __('To_type 2'),'1' => __('To_type 1')];
    }

    public function getDataTypeList()
    {
        return ['2' => __('Data_type 2'),'1' => __('Data_type 1')];
    }

    public function getConfirmTypeList()
    {
        return ['1' => __('Confirm_type 1'), '2' => __('Confirm_type 2'), '0' => __('Confirm_type 0')];
    }

    public function getProductIdAttr($value, $data){
        $list = Constant::$productName;
        return isset($list[$value]) ? $list[$value] : "未知";
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFromTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['from_type']) ? $data['from_type'] : '');
        $list = $this->getFromTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getToTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['to_type']) ? $data['to_type'] : '');
        $list = $this->getToTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDataTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['data_type']) ? $data['data_type'] : '');
        $list = $this->getDataTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getConfirmTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['confirm_type']) ? $data['confirm_type'] : '');
        $list = $this->getConfirmTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getConfirmTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['confirm_time']) ? $data['confirm_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setConfirmTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
